$(function(){

    listRole();
    
    $("#dialog-form-add").dialog( { // 修改form的情况
		autoOpen : false,
		height : 300,
		width : 450,
		modal : true,
		buttons : {
			'提交' : function() {
    	var str = $("#add_name").attr("value");
    	url_addRole = "oa-role/tbRoleAction_addRold.action";
		req_addRole = {
			'tbRole.name':str
		};
		$.post(url_addRole,req_addRole,function(data){
			alert(data.message);
            listRole();
		});
		$('#dialog-form-add').dialog('close');
			},
			'取消' : function() {
				$('#dialog-form-add').dialog('close');
			}
		},
		close : function() {
			$('#dialog-form-add').dialog('close');
		}
	});
    
    
});


function listRole(){
	
	$("#listRole").empty();
    url_queryAllRoles = "oa-role/tbRoleAction_queryAllRoles.action";
    req_queryAllRoles = {};
    $.post(url_queryAllRoles, req_queryAllRoles, function(data){
        if (data.tbRoles.length > 0) { //有数据时
            $("#loadingRole").css("display", "none");
        }
        else {
            $("#loadingRole").css("display", "none");
            $("#noneRole").css("display", "block");
        }
        $.each(data.tbRoles, function(i, n){
        
            $("#listRole").append("<tr>" +
            "<td  bgcolor='#FFFFFF' class = 'roleId'>" +
            "" +
            n.tbRoleId +
            "</td>" +
            "<td  bgcolor='#FFFFFF'>" +
            "<span class = 'roleName'>" +
            n.name +
            "</span></td>" +
            "<td  bgcolor='#FFFFFF'>" +
            "<input type='button' class='see' value='查看' /></td>" +
            "<td  bgcolor='#FFFFFF'>" +
            "<input type='button' class='delete' value='删除' /></td>" +
            
            "</tr>");
        });
        
        $(".see").click(function(){
            var roleId = $(this).parent().siblings(".roleId").text();
            location.href = "/hg/oa/permission/permissionTree.jsp?theroleId=" + roleId;
        });
        
        $(".delete").click(function(){
        
            var roleId = $(this).parent().siblings(".roleId").text();
            url_deleteRoleById = "oa-role/tbRoleAction_deleteRoleById.action";
            req_deleteRoleById = {
                'tbRole.tbRoleId': roleId
            };
            $.post(url_deleteRoleById, req_deleteRoleById, function(data){
                alert(data.message);
                listRole();
            });
        });
        
        $("#addRole").click(function(){
        	$('#dialog-form-add').dialog('open');
		});
    });
}
